<?php

class CS_Dispatcher extends CS_Core
{
	public static $instance;

	public static function getInstance()
	{
		$class = __CLASS__;
		
		if(!isset(self::$instance[$class]))
		{
			self::$instance[$class] = new $class;
		}

		return self::$instance[$class];
	}

	public static function dispatch()
	{
		$args = null;
		if(isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO']))
		{
			$args = explode('/', $_SERVER['PATH_INFO']);

			if(empty($args[0]))
			{
				unset($args[0]);
			}

			if(isset($args[1]) && !empty($args[1]))
			{
				$controller_name = $args[1];
				unset($args[1]);
			}

			if(isset($args[2]) && !empty($args[2]))
			{
				$action = $args[2];
				unset($args[2]);
			} else {
				$action = 'index';
			}
		} else {
			// set default controller
			$controller_name = self::$default_controller;
			$action = self::$default_action;
		}

		$app = new CS_Application;
		$app->init($controller_name, $action, $args);
	}
}
